# coding: utf-8
from django.db import models
from django.utils.encoding import python_2_unicode_compatible
from DjangoUeditor.models import UEditorField
from django.core.urlresolvers import reverse


@python_2_unicode_compatible
class Categories (models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField('分类名称', max_length=256)
    slug = models.CharField('分类网址', max_length=256, db_index=True)
    intro = models.TextField('分类简介', default='')

    def __str__(self):
        return self.name

    def get_absolute_url(self):
        return reverse('category', args=(self.slug,))

    class Meta:
        verbose_name = '分类'
        verbose_name_plural = '分类'
        ordering = ['name']  # 按照哪个分类排序


@python_2_unicode_compatible
class Article (models.Model):
    categories = models.ManyToManyField(Categories, verbose_name='所属分类')
    id = models.AutoField(primary_key=True)
    title = models.CharField('标题', max_length=256)
    slug = models.CharField('网址', max_length=256, db_index=True)
    author = models.ForeignKey('auth.User', blank=True, null=True, verbose_name='作者')
    content = UEditorField('内容', height=300, width=1000,
                           default=u'', blank=True, imagePath='uploads/images/',
                           toolbars='besttome', filePath='uploads/files/')
    published = models.BooleanField('正式发布', default=True)

    pub_date = models.DateTimeField('发标时间', auto_now_add=True, editable=True)
    update_time = models.DateTimeField('更新时间', auto_now=True, null=True)

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse('article', args=(self.id,))

    class Meta:
        verbose_name = '文章'
        verbose_name_plural = '文章'
